#include "clusternode.hpp"

ClusterNode::ClusterNode(Cluster *cluster) {
  this->cluster=cluster;
  cluster->setNode(this);
}

ClusterNode::~ClusterNode() {
  QMutableListIterator<ClusterNode *> i(children);
  while (i.hasNext()) {
    ClusterNode *child=i.next();
    delete child;
    i.remove();
  }
}

ClusterNode::ClusterNode(const Vector3d &position,Cluster *cluster) {
  this->position=position;
  this->cluster=cluster;
  cluster->setNode(this);
}

void ClusterNode::setCluster(Cluster *cluster) {
  this->cluster=cluster;
}

void ClusterNode::setPosition(const Vector3d &position) {
  this->position=position;
}

void ClusterNode::calculatePosition(const Cluster *otherCluster) {
  double exp=cluster->getSize()<=otherCluster->getSize()
    ? cluster->getSize()/otherCluster->getSize()
    : otherCluster->getSize()/cluster->getSize();
  position=otherCluster->getNode()->getPosition()
    +pow(0.5,exp)*(cluster->getCentreOfGravity()-otherCluster->getNode()->getPosition());
}

void ClusterNode::addChild(ClusterNode *child) {
  children.append(child);
  child->setParent(this);
}

void ClusterNode::setParent(ClusterNode *parent) {
  this->parent=parent;
}

const Vector3d & ClusterNode::getPosition() const {
  return position;
}

const QList<ClusterNode *> & ClusterNode::getChildren() const {
  return children;
}
